package com.itheima.reggie.handler;

import com.itheima.reggie.comon.CustomException;
import com.itheima.reggie.comon.ResultInfo;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    //处理异常
    @ExceptionHandler(DuplicateKeyException.class)
    public ResultInfo FuckNameRepetition(DuplicateKeyException de) {
        //打印异常信息
        de.printStackTrace();
        if (Objects.requireNonNull(de.getMessage()).contains("idx_category_name")) {
            return ResultInfo.error("名字重复,请更换后重试~");
        }
        return ResultInfo.error("错了,全错了!!!");
    }

    @ExceptionHandler(CustomException.class)
    public ResultInfo FuckDeleteCategory(CustomException ce) {
        //打印异常
        ce.printStackTrace();
        return ResultInfo.error(ce.getMessage());
    }
}
